<?php
class Memo_model extends Model{
  function Memo_model(){
    parent::Model();
  }
  
  function list_header($ro=false) {
    $s = "<table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tbl\">".
        "<thead><tr><th>No</th><th>Tanggal</th><th>Pengirim</th><th>Tujuan</th><th>Perihal</th>";
    if ($ro==false) {
      $s .= "<th colspan=3> Aksi </th>";
    }
    $s .= "</tr></thead><tbody>";
    return $s;
  }
  
  function list_isi($where="", $order="", $ro=false,$preview=false) {
    
    $this->db->where('pengirim = ', $this->session->userdata('username'));
    $this->db->or_where('tujuan = ', $this->session->userdata('username'));
    if ($where!='') {
      $this->db->where($where);
    }
    if ($order == "") {
      $this->db->order_by('tanggal desc');
    } else {
      $this->db->order_by($order);
    }
    $q = $this->db->get('memo');
    
    $s = "[";
    $i = 1;
    $order   = array("\r\n", "\n", "\r", "'");
    $replace = ' ';
    
    foreach($q->result() as $row) {
      if ($i % 2 == 1) {
    $kelas = "event2";
      } else {
    $kelas = "event";
      }
      
      $s .= "['<tr class=\"$kelas\" id=\"row-".$row->id."\" ><td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>".
          "<td><div align=\"center\">".YMD2Indo(trimTime($row->tanggal), TGL_SEPARATOR )."</div></td>".
          "<td>".$row->pengirim."</td>".
          "<td>".$row->tujuan."</td>".
          "<td>".str_replace($order, $replace, $row->perihal)."</td>";
      //"<td><div align=\"center\">".($row->email_sent==1?ikonCeklist():ikonCakra())."</div></td>";
      #if ($this->session->userdata('role') <= GROUP_MANAJEMEN)  {
    $s .= "<td><div align=\"center\"> <a href=\"".base_url()."index_arsip.php/memo/form_edit/".$row->id."/true\" title=\"Preview\" class=\"edit\"><img src=\"".base_url()."templates/images/printview.gif\" border=\"0\"></a></div></td>";
        #}
        if ($this->session->userdata('username') == $row->pengirim)  {
    $s .= "<td><div align=\"center\"> <a href=\"".base_url()."index_arsip.php/memo/form_edit/".$row->id."\" title=\"Ubah Data\" class=\"edit\"><img src=\"".base_url()."templates/images/edit.gif\" border=\"0\"></a></div></td>";
      #}
      #  if ($this->session->userdata('role') <= GROUP_SA)  {   
    $s .= "<td><div align=\"center\"><a href=\"javascript:deleteRow(".$row->id.")\"><img src=\"".base_url()."templates/images/del.gif\" border=\"0\"></a></div></td>";
      } 
      $s .= "</tr>'],";
      
      $i++;
    }
    $s = substr($s, 0, -1);
    $s .= '];';
    return $s;
  }
  
  function entry_form($form_id, $form_action, $arr=array(),$preview=false) {
    //format tgl_lahir harus YMD
    if (isset($arr['tanggal'])) {
      $a = explode(TGL_SEPARATOR, trimTime($arr['tanggal']));
      $d = $a[2] * 1;
      $m = $a[1] * 1;
      $y = $a[0];
    } else {
      $d = date("j");
      $m = date("n");
      $y = date("Y");
    }
    
    if($preview==true){
      $tanggal = isset($arr['tanggal'])?$arr['tanggal']:date('d/m/Y');
      $pengirim = isset($arr['pengirim'])?$arr['pengirim']:$this->session->userdata('username');
      $tujuan = isset($arr['tujuan'])?$arr['tujuan']:"";
      $perihal = isset($arr['perihal'])?$arr['perihal']:"";
      $isi = isset($arr['isi'])?$arr['isi']:"";
      $simpan = " ";
      
    }  else {
      $tanggal = "<input id=\"tanggal\" name=\"tanggal\" class=\"date-pick dp-applied\" size=\"38\" value=\"".(isset($arr['tanggal'])?$arr['tanggal']:date('d/m/Y'))."\" />";
      $pengirim = "<input type=\"text\" name=\"pengirim\" id=\"pengirim\" size=\"38\" value=\"".(isset($arr['pengirim'])?$arr['pengirim']:$this->session->userdata('username'))."\" readonly=\"readonly\"  />";
      $tujuan = "<input type=\"text\" name=\"tujuan\" id=\"tujuan\" size=\"38\" value=\"".(isset($arr['tujuan'])?$arr['tujuan']:"")."\" />"; 
      $perihal = "<input type=\"text\" name=\"perihal\" id=\"perihal\" size=\"38\" value=\"".(isset($arr['perihal'])?$arr['perihal']:"")."\" />";
      $isi = "<textarea name=\"isi\" id=\"isi\" cols=\"45\" rows=\"5\">".(isset($arr['isi'])?$arr['isi']:"")."</textarea>";
      $simpan = "<input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"   Simpan   \" />";
    }  
    
    $ret = "
            <form id=\"".$form_id."\" action=\"".$form_action."\" method=\"post\" >
            <fieldset >
            <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
              <tr>
                <td class=\"kolom\" valign=\"top\">Tanggal </td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">$tanggal</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Pengirim</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\">$pengirim</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Tujuan</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\">$tujuan</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Perihal</td>
                <td valign=\"top\" width=\"5\">:</td>
                <td valign=\"top\">$perihal</td>
              </tr>
              <tr>
                <td valign=\"top\">Isi Memo</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">$isi</td>
              </tr>
        ";
    if (isset($arr['created_by'])) {
      $ret .= "
              <tr>
                <td class=\"kolom\" valign=\"top\">Dibuat oleh</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><i>".(isset($arr['created_by'])?$arr['created_by']:"")."</i></td>
              </tr>";
    }
    if (isset($arr['last_update'])) {
      $ret .= "
              <tr>
                <td class=\"kolom\" valign=\"top\">Terakhir di update oleh</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><i>".(isset($arr['last_update'])?$arr['last_update']:"")."</i></td>
              </tr>";
    }
    $ret .= "<tr>
                <td valign=\"top\">&nbsp;</td>
                <td valign=\"top\">&nbsp;</td>
                <td valign=\"top\">
                  <input class=\"tombol\" type=\"button\" name=\"btn_kembali\" id=\"btn_kembali\" value=\"<< Kembali\" />
        $simpan
                </td>
            </tr>
            </table>
            </fieldset>
            </form>
        ";
    
    return $ret;
  }
  
  function simpan($arr, $is_update=false, $id='') {
    if ($is_update==FALSE) {
      $this->db->insert('memo', $arr);
    } else {
      $arr['tgl_update'] = date('Y-m-d H:i:s');
      $this->db->where('id', $id);
      $this->db->update('memo', $arr);
    }
  }
  
  function list_publik() { 
    //pta_id_kategori_arsip != 1 AND 
    $this->db->where('pta_id_kategori_arsip != 1 AND published = 1 AND kode_viewer = 0' );
    $this->db->group_by('pta_id_kategori_arsip');
    $this->db->order_by('tgl_dokumen desc');
    $q = $this->db->get('arsip');           
    $order   = array("\r\n", "\n", "\r", "'");
    $replace = ' ';
    $this->load->model('Datum_model');
    
    
    $s="";             
    foreach($q->result() as $row) {
      $id = $row->pta_id_kategori_arsip;       
      $kategori = Datum_model::getData('nama', 'kategori_arsip', array('idpta_kategori_arsip' => $row->pta_id_kategori_arsip));
      $j = $this->db->count_all('arsip WHERE pta_id_kategori_arsip = '.$row->pta_id_kategori_arsip.' AND kode_viewer = 0 AND published = 1');
      
      $s .= "<a class='menuitem submenuheader' href='#'><span>($j)</span>$kategori</a>".
          "<div class='submenu'><ul> ";
      
      
      $this->db->where("pta_id_kategori_arsip = $id AND published = 1 AND kode_viewer = 0");                    
      $this->db->order_by('tgl_dokumen desc');
      $this->db->limit(10); 
      $q2 = $this->db->get('arsip');
      
      foreach($q2->result() as $r) {
    
    $no = $r->idpta_arsip;
    $namafile = Datum_model::getData('namafile', 'files', array('idpta_files' => $r->pta_id_files));
    
    $ext = end(explode('.', $namafile));        
    
    $s .= "<li><a href='".base_url()."arsip/unduh_arsip/".$no."' target='_blank'>".str_replace($order, $replace, $r->judul)."</a></li>";
    
      }
      
      $s .= "</div></ul>";
      
      
    }
    
    $s .= " ";    
    return $s;
  }
  
  function arsip_baru($where="",$limit=true) { 
    if ($where!='') {
      $this->db->where($where);
    }
    if ($limit==true) {
      $this->db->limit(10);
    }
    //pta_id_kategori_arsip != 1 AND 
    
    
    $order   = array("\r\n", "\n", "\r", "'");
    $replace = ' ';
    $this->load->model('Datum_model');
    
    
    $s='';             
    
    $this->db->where("pta_id_kategori_arsip != 1 AND published = 1 AND kode_viewer = 0");                    
    $this->db->order_by('tgl_dokumen desc');
    
    $q2 = $this->db->get('arsip');
    
    foreach($q2->result() as $r) {
      $tgli = strtotime($r->tgl_input);       
      $no = $r->idpta_arsip;
      $namafile = Datum_model::getData('namafile', 'files', array('idpta_files' => $r->pta_id_files));
      switch ($r->kode_viewer) {
    case VIEWER_EVERYBODY: $viewer = 'Semua'; break;
    case VIEWER_GRUP: $grup = Datum_model::getData('nama', 'grup', array('idpta_grup' => $r->viewer));
      $viewer = 'Grup '.$grup; break;
    case VIEWER_USER_TTT: $viewer = 'User '.$r->viewer;
      } 
      $ext = end(explode('.', $namafile)); 
      
      $s .='<style type="text/css">
                        #ars-'.$no.' {
                    height: 100%;
                    width: 44%;
                    background: #000000;
                    position: absolute;
                    top: 0;
                    -moz-opacity: 0.75;
                    -khtml-opacity: 0.75;
                    opacity: 0.75;
                    filter:alpha(opacity=75);
                    } 
                    </style>'; 
      
      $s .= "<div class='judul'><a href='#'>".str_replace($order, $replace, $r->judul)."</a></div>
    <div class='docinfo'><span style='float:right'>".strftime('%d/%m/%Y',$tgli)."</span>Hits : ".$r->hits."<br /><a href='#' onClick='Show_Popup($no)'>Detail</a> | <a href='".base_url()."arsip/unduh_arsip/".$no."' target='_blank' >Download</a> </div><hr />";
      
      $this->db->where('idpta_files = '.$r->pta_id_files.'');                    
      $this->db->order_by('tgl_input desc');
      $this->db->limit(1); 
      $q = $this->db->get('files');
      foreach($q->result() as $r2) {   
    
    $s .='<div id="popup" style="display: none;"></div>
<div id="ars-'.$no.'" style="display: none;" >
<div id="popup_content" ><a href="#" onclick="Close_Popup('.$no.');">Close</a> <br>

<table class="info-detail" width="100%" border="0" cellpadding="2" cellspacing="1">
      <tr>
        <td class="title_info">Name</td>
        <td class="dot">:</td>
        <td>'.$r->judul.'</td>
      </tr>
      <tr>
        <td class="title_info">Description</td>
        <td class="dot">:</td>
        <td>'.$r->deskripsi.'</td>
      </tr>
      <tr>
        <td class="title_info">Filename</td>
        <td class="dot">:</td>
        <td>'.$r2->namafile.'</td>
      </tr>
      <tr>
        <td class="title_info">Filesize</td>
        <td class="dot">:</td>
        <td>'.$r2->ukuran.'</td>
      </tr>
      <tr>
        <td class="title_info">Filetype</td>
        <td class="dot">:</td>
        <td>'.$r2->ekstensi.' ( '.$r2->tipe_file.')</td>
      </tr>
      <tr>
        <td class="title_info">Created On</td>
        <td class="dot">:</td>
        <td>'.$r2->tgl_input.'</td>
      </tr>
      <tr>
        <td class="title_info">Viewers</td>
        <td class="dot">:</td>
        <td>'.$viewer.'</td>
      </tr>
      <tr>
        <td class="title_info">Hits</td>
        <td class="dot">:</td>
        <td>'.$r->hits.'</td>
      </tr>
    </table>


</div>
</div> ';               
      }      }
    
    $s .= ' ';
    
    
    return $s;
  }
  
  
  function kirim_email($email, $judul, $isi, $id) {
    $this->load->library('email');
    $this->email->from('mugi_s@pusat.itb.ac.id');
    $this->email->to($email);
    $this->email->subject('[MEMO-arsipitb] '.$judul);
    $this->email->message($isi);
    if ( ! $this->email->send()) {
      $arr['email_sent'] = '0';
    } else {
      $arr['email_sent'] = '1';
    }
    $this->db->where('id', $id);
    $this->db->update('memo', $arr);
  }
  
}
?>